package com.wanmait.recruitment.webController;

import com.wanmait.recruitment.pojo.Company;
import com.wanmait.recruitment.pojo.Message;
import com.wanmait.recruitment.pojo.Userinfo;
import com.wanmait.recruitment.service.*;
import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.*;
import java.util.UUID;

@Controller
public class CommonController {
    @Resource
    private MessageService messageService;
    @Resource
    private SubscribeService subscribeService;
    @Resource
    private CompanyService companyService;
    @Resource
    private UserinfoService userinfoService;

    //获取公司消息数量
    @RequestMapping("zhuang/common/ajaxCompany")
    @ResponseBody
    public String getCount(HttpServletRequest request)
    {
        HttpSession session = request.getSession();
        Company company = (Company)session.getAttribute("company");
        Message message = new Message();
        message.setContent("投递");
        message.setCompanyId(company.getId());
        String count = String.valueOf(messageService.findCompanyId(message));
        return count;
    }
    //获取用户投递消息数量
    @RequestMapping("zhuang/common/ajaxCount")
    @ResponseBody
    public String getcount(HttpServletRequest request)
    {
        HttpSession session = request.getSession();
        Userinfo userinfo = (Userinfo) session.getAttribute("userinfo");
        Message message = new Message();
        message.setUserinfoId(userinfo.getId());
        message.setContent("投递");
        int c = messageService.findCount(message);
        String count = String.valueOf(c);
        return count;
    }
    @RequestMapping("zhuang/upload/uploadAfter")
    public void getUploadAfter(){}
    @GetMapping("zhuang/upload/uploadPic")
    public void getUpload(){}
    @PostMapping("zhuang/upload/uploadPic")
    public String upload(HttpServletRequest request, MultipartFile file){
        String fileName = "";
        if(file!=null && !file.isEmpty()) {
            //文件名
            String originalFilename = file.getOriginalFilename();
            //扩展名
            String ext = FilenameUtils.getExtension(originalFilename);
            if(!",jpg,jpeg,png,gif,".contains(","+ext+",")){
                return "redirect:/zhuang/upload/uploadPic?msg=ext";
            }
            if(file.getSize()>10240000){
                return "redirect:/zhuang/upload/uploadPic?msg=size";
            }
            //物理路径
            String realPath = request.getServletContext().getRealPath("/static/uploadImg");
            fileName = UUID.randomUUID()+"."+ext;
            File outFile = new File(realPath,fileName);
            try {
                file.transferTo(outFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "redirect:/zhuang/upload/uploadAfter?fileName="+fileName;
    }
    @RequestMapping("zhuang/upload/delete")
    @ResponseBody
    public String delete(String fileName,HttpServletRequest request){
        String realPath = request.getServletContext().getRealPath("/static/uploadImg/"+fileName);
        File file = new File(realPath);
        String mes = "";
        if(file.exists()&&file.isFile()){
            file.delete();
            mes = "deleteSuccess";
        }
        return mes;
    }
    @RequestMapping("zhuang/include/subscribeCount")
    @ModelAttribute("subscribeCount")
    public Integer subscribeCount(){
        return subscribeService.findSubscribeCount();
    }
    @RequestMapping("zhuang/about")
    public void about(){}
    @RequestMapping("zhuang/accountBind")
    public void accountBind(){}
    @GetMapping("zhuang/updatePwd")
    public void getUpdatePwd(){}
    @PostMapping("zhuang/updatePwd")
    public String updatePwd(String comfirmpassword,HttpSession session){
        Company company = (Company)session.getAttribute("company");
        Userinfo userinfo = (Userinfo)session.getAttribute("userinfo");
        if(company!=null){
            companyService.updatePassword(comfirmpassword,company.getId());
        }
        if(userinfo!=null){
            userinfoService.updatePassword(comfirmpassword,userinfo.getId());
        }
        return "redirect:/zhuang/login";
    }

}
